package com.oreilly.ez.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableConfigurationProperties({YmlConfig.class})
public class DefaultView implements WebMvcConfigurer {


    @Autowired
    private YmlConfig ymlConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        InterceptorRegistration addInterceptor = registry.addInterceptor(new SysInterceptor());

        addInterceptor.addPathPatterns("/**");
        addInterceptor.excludePathPatterns("/error");
//        addInterceptor.excludePathPatterns("/login**");
//        addInterceptor.excludePathPatterns("/toLogin");
//        addInterceptor.excludePathPatterns("/index.html");
//        addInterceptor.excludePathPatterns("/login.html");
//        addInterceptor.excludePathPatterns("/fail.html");
        addInterceptor.excludePathPatterns("/**.js");
        addInterceptor.excludePathPatterns("/**.png");
        addInterceptor.excludePathPatterns("/**.css");
        addInterceptor.excludePathPatterns("/**.eot");
        addInterceptor.excludePathPatterns("/**.ttf");
        addInterceptor.excludePathPatterns("/**.woff");
        addInterceptor.excludePathPatterns("/browser/index.html#/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

//        registry.addViewController("/").setViewName("forward:/index");
//        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
//        super.addViewControllers(registry);


    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

//        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
//        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
//        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

        registry.addResourceHandler("/**")
                .addResourceLocations("META-INF/resources/",
                        "classpath:/resources/",
                        "classpath:/static/",
                        "classpath:/public/",
                        "classpath:/templates/",
                        "file:" + ymlConfig.getUploadDir(),
                        "file:" + ymlConfig.getConfDir(),
                        "file:" + ymlConfig.getPages()

                        );

    }
}
